package com.bwie.controller;

import com.bwie.config.FreemarkerUtil;
import com.bwie.config.TouristInfo;
import com.bwie.pojo.Tourist;
import com.bwie.utils.R;
import freemarker.template.TemplateException;
import org.apache.commons.lang.StringUtils;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.IndexOperations;
import org.springframework.data.elasticsearch.core.SearchHit;
import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.data.elasticsearch.core.document.Document;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @Auther:${涛}
 * @Date:2024/8/31 09:15
 * @Description: TouristController
 * @Version 1.0.0
 */
@RestController
@RequestMapping("/user/tree")
public class TouristController {


    @Autowired
    private ElasticsearchRestTemplate elasticsearchRestTemplate;


    @PostMapping("/save")
    public R save() {
        IndexOperations indexOperations = elasticsearchRestTemplate.indexOps(TouristInfo.class);
        boolean exists = indexOperations.exists();
        if (exists) {
            indexOperations.delete();
        }

        indexOperations.create();

        Document mapping = indexOperations.createMapping();
        indexOperations.putMapping(mapping);
        return R.OK("创建成功");
    }

    //es查询数据

    @PostMapping("/touListEs")
    public R touListEs(@RequestBody Tourist tourist) throws TemplateException, IOException {
        BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();

        //查询所有
//        boolQueryBuilder.must(QueryBuilders.matchAllQuery());

        //高亮查询
        if (!StringUtils.isEmpty(tourist.getTouristName())) {
            boolQueryBuilder.must(QueryBuilders.matchQuery("touristName", tourist.getTouristName()));
        }


        //区间查询
        if (!StringUtils.isEmpty(tourist.getBeginTime()) && !StringUtils.isEmpty(tourist.getBeginTime())) {
            boolQueryBuilder.must(QueryBuilders.rangeQuery("time").format("yyyy-MM-dd HH:mm:ss").timeZone("GMT+8").gt(tourist.getBeginTime()).lt(tourist.getEndTime()));
        }

        int pageNum = 0;
        if (tourist.getPageNum() > 0) {
            pageNum = tourist.getPageNum() - 1;
        }
        PageRequest request = PageRequest.of(pageNum, tourist.getPageSize());

        //高亮设置颜色
//        HighlightBuilder highlightBuilder = new HighlightBuilder();
        HighlightBuilder highlightBuilder = new HighlightBuilder();

        highlightBuilder.field("touristName").preTags("<font color=red>").postTags("</font>");

        NativeSearchQueryBuilder nativeSearchQueryBuilder = new NativeSearchQueryBuilder();
        nativeSearchQueryBuilder.withPageable(request).withHighlightBuilder(highlightBuilder).withQuery(boolQueryBuilder);

        SearchHits<TouristInfo> searchHits = elasticsearchRestTemplate.search(nativeSearchQueryBuilder.build(), TouristInfo.class);

        long total = searchHits.getTotalHits();
        ArrayList<TouristInfo> tourists = new ArrayList<>();
        for (SearchHit<TouristInfo> searchHit : searchHits.getSearchHits()) {
            TouristInfo content = searchHit.getContent();
            List<String> name = searchHit.getHighlightField("touristName");
            for (String s : name) {
                if (name != null && name.size() > 0) {
                    name.get(0);
                    content.setTouristName(s);
                }
            }
            tourists.add(content);
        }


        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("total", total);
        hashMap.put("pageNum", pageNum + 1);
        hashMap.put("pageSize", tourist.getPageSize());
        hashMap.put("tourists", tourists);


        String url = "D:\\test\\file\\aa" + ".html";

        FreemarkerUtil freemarkerUtil = new FreemarkerUtil();
        freemarkerUtil.createHtml(hashMap, "freemark", url);
        return R.OK(hashMap);


    }
    public void test01(){
        System.out.println("dwaadwadwa");
    }

}


